package org.jwcloud.project.module.zx.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.StringUtils;

import com.jw.mid.mm.comm.common.pagination.Pagination;

/**
 * 前端交互用格式化工具类
 * @author song
 *
 */
public class FmtUtil {
	public static List<Object> buildListCommon(List<Map<String, String>> list, String flag) {
		List<Object> res = new ArrayList();
		for(Map<String, String> map: list) {
			res.add(map.get(flag));
		}
		return res;
	}
	public static Map buildResCommon(List<Map<String, String>> list, String... flag) {
		if(list == null) {
			throw new RuntimeException("list为空");
		}
		if(flag == null || flag.length == 0 || flag.length % 2 == 1) {
			throw new RuntimeException("参数错误");
		}
		
		Map res = new HashMap();
		for(int i = 0; i < flag.length; i += 2) {
			res.put(flag[i], buildListCommon(list, flag[i + 1]));
		}
		
		return res;
	}
	/**
	 * 无法用注解时的替代方案
	 * @param map
	 * @return
	 */
	public static String nullRes(Map map, String... strs) {
		if(strs == null || strs.length == 0 || strs.length % 2 == 1) {
			return "";
		}
		
		String res = "";
		for(int i = 0; i < strs.length; i += 2) {
			if(StringUtils.isEmpty(map.get(strs[i]))) {
				res += "【" + strs[i + 1] + "】不可为空；";
			}
		}
		
		return res;
	}
	
	/**
	 * 临时   之后改切片、pagehelper
	 * @param map
	 */
	public static void pagiBuild(Pagination pagination, Map<String, String> param) {
		String pagisize = param.get("pageSize");
		String currentPage = param.get("currentPage");
		if(StringUtils.isEmpty(pagisize) || StringUtils.isEmpty(currentPage)) {
			return;
		}
		int ipagisize = Integer.valueOf(pagisize);
		int icurrentPage = Integer.valueOf(currentPage);
		param.put("_length", pagisize);
		int start = (icurrentPage - 0) * ipagisize;
		param.put("_start", start + "");
		pagination.setCurrentPage(icurrentPage);
		pagination.setPageSize(ipagisize);
		
		
		System.out.println("------------------");
		System.out.println(param);
		
		
//		String pagisize = param.get("pagisize");
//		String currentPage = param.get("currentPage");
//		if(StringUtils.isEmpty(pagisize) || StringUtils.isEmpty(currentPage)) {
//			return;
//		}
//		int ipagisize = Integer.valueOf(pagisize);
//		int icurrentPage = Integer.valueOf(currentPage);
//		param.put("_length", pagisize);
//		//int start = (icurrentPage - 1) * ipagisize;
//		int start = (icurrentPage - 0) * ipagisize;
//		param.put("_start", start + "");
//		pagination.setCurrentPage(icurrentPage);
//		pagination.setPageSize(ipagisize);
	}
	
	public static void main(String[] args) {
		Map map = new HashMap();
		map.put("aaa", "asdfg");
		
		System.out.println(nullRes(map, "aaa", "AAA", "bbb", "BBB"));
	}
}
